package com.mashiro.system.controller;

import com.mashiro.common.annotation.Log;
import com.mashiro.common.core.controller.BaseController;
import com.mashiro.common.core.domain.AjaxResult;
import com.mashiro.common.core.page.TableDataInfo;
import com.mashiro.common.enums.BusinessType;
import com.mashiro.common.utils.poi.ExcelUtil;
import com.mashiro.system.domain.MoeMessage;
import com.mashiro.system.domain.query.ReadMessageQuery;
import com.mashiro.system.service.IMoeMessageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息Controller
 *
 * @author mashiro
 * @date 2023-04-14
 */
@RestController
@RequestMapping("/system/message")
public class MoeMessageController extends BaseController {
    @Resource
    private IMoeMessageService moeMessageService;

    /**
     * 查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(MoeMessage moeMessage) {
        startPage();
        List<MoeMessage> list = moeMessageService.selectMoeMessageList(moeMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoeMessage moeMessage) {
        List<MoeMessage> list = moeMessageService.selectMoeMessageList(moeMessage);
        ExcelUtil<MoeMessage> util = new ExcelUtil<MoeMessage>(MoeMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(moeMessageService.selectMoeMessageById(id));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoeMessage moeMessage) {
        return toAjax(moeMessageService.insertMoeMessage(moeMessage));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoeMessage moeMessage) {
        return toAjax(moeMessageService.updateMoeMessage(moeMessage));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(moeMessageService.deleteMoeMessageByIds(ids));
    }

    /**
     * 根据用户获取未读消息数量
     *
     * @param userId 用户
     * @return 结果
     */
    @GetMapping("/count/{userId}")
    public AjaxResult countByUserId(@PathVariable Long userId) {
        return success(moeMessageService.countByUserId(userId));
    }

    /**
     * 批量修改消息状态
     *
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @PostMapping("/read")
    public AjaxResult read(@RequestBody ReadMessageQuery readMessageQuery) {
        return success(moeMessageService.read(readMessageQuery.getUserId(), readMessageQuery.getMessageIds()));
    }
}
